import 'dart:io';
import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'package:path_provider/path_provider.dart';

class PdfGenerator {
  static Future<File> generatePdf() async {
    // 创建一个新的 PDF 文档
    final pdf = pw.Document();

    // 添加文本内容
    pdf.addPage(
      pw.Page(
        build: (pw.Context context) {
          return pw.Center(
            child: pw.Column(
              mainAxisAlignment: pw.MainAxisAlignment.center,
              children: [
                pw.Text(
                  '这是一个示例 PDF 文件',
                  style: const pw.TextStyle(fontSize: 20),
                ),
                pw.SizedBox(height: 20),
                pw.Text(
                  '由 Flutter 生成',
                  style: const pw.TextStyle(fontSize: 16),
                ),
              ],
            ),
          );
        },
      ),
    );

    // 保存 PDF 文件到本地
    final output = await getTemporaryDirectory();
    final file = File('${output.path}/example.pdf');
    await file.writeAsBytes(await pdf.save());

    return file;
  }
}